package com.summer.content.api;


import com.summer.commonException.ValidationGroups;
import com.summer.commonException.XueChengException;
import com.summer.content.service.CourseBaseService;
import com.summer.dto.AddCourseDto;
import com.summer.dto.CourseBaseInfoDto;
import com.summer.dto.EditionDto;
import com.summer.dto.QueryCourseParamsDto;
import com.summer.entity.XcUser;
import com.summer.model.PageParams;
import com.summer.model.PageResult;
import com.summer.pojo.CourseBase;
import com.summer.utils.CurrentLoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(value = "课程控制器API", tags = "课程控制器API")
@RestController
public class CourseController {

    @Autowired
    private CourseBaseService courseBaseService;

    @ApiOperation(value = "分页条件查询API")
    @PostMapping("/course/list")
    @ApiImplicitParam(name = "queryCourseParamsDto", value = "查询条件", required = false)
    @PreAuthorize("hasAuthority('xc_teachmanager_course_list')")
    public PageResult<CourseBase> getByPageAndCondition(PageParams pageParams,
            @RequestBody(required = false) QueryCourseParamsDto queryCourseParamsDto){
        log.info("【分页条件查询API】");
        // 获取当前用户id，用于细粒度数据权限控制
        XcUser loginUser = CurrentLoginUser.getLoginUser();
        if (loginUser == null){
            throw new XueChengException("用户信息有误");
        }
        String companyId = loginUser.getCompanyId();
        return courseBaseService.getByConditionAndPage(pageParams, queryCourseParamsDto, companyId);
    }

    @ApiOperation(value = "根据id查询课程基本信息与营销信息")
    @ApiImplicitParam(name = "id", value = "课程id", required = true)
    @GetMapping("course/{id}")
    public CourseBaseInfoDto getBaseAndMarketInfo(@PathVariable Long id){
        log.info("【根据id查询课程基本信息与营销信息】");
        return courseBaseService.getBaseAndMarketInfo(id);
    }


    @ApiOperation(value = "添加课程API")
    @PostMapping("course")
    @ApiImplicitParam(name = "dto", value = "添加课程dto", required = true)
    public CourseBaseInfoDto addCourseBase(@RequestBody @Validated(ValidationGroups.Insert.class) AddCourseDto dto){
        log.info("【添加课程API】");
        Long companyId = 1232141425L;
        return courseBaseService.addCourseBase(companyId, dto);
    }

    @ApiOperation(value = "修改课程API")
    @PutMapping("course")
    public CourseBaseInfoDto updateCourse(@RequestBody @Validated(ValidationGroups.Update.class) EditionDto dto){
        log.info("【修改课程API】");
        Long companyId = 1232141425L;
        return courseBaseService.updateCourse(companyId, dto);
    }

    @ApiOperation(value = "删除课程")
    @DeleteMapping("course/{id}")
    public void deleteCourse(@PathVariable Long id){
        log.info("【删除课程API】 id = {}", id);
        courseBaseService.deleteCourse(id);
    }
}
